package net.sxnic.item.mdata.action;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sxinfo.core.dao.Page;
import net.sxnic.comm.CommConstant;
import net.sxnic.comm.attachment.AttachmentManager;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.comm.utils.WordUtils;
import net.sxnic.item.ItemActionSupport;
import net.sxnic.item.duiying.DuiyingManager;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.item.mdata.MdataManager;
import net.sxnic.item.util.PageUtils;
import net.sxnic.jhitem.itemannex.ItemAnnex;
import net.sxnic.jhitem.itemannex.ItemAnnexManager;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemclob.ItemClobManager;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itemfund.ItemFundManager;
import net.sxnic.jhitem.itemgjhzunit.ItemGjhzunit;
import net.sxnic.jhitem.itemgjhzunit.ItemGjhzunitManager;
import net.sxnic.jhitem.itemoperation.ItemOperationManager;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itempeople.ItemPeopleManager;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemprincipal.ItemPrincipalManager;
import net.sxnic.jhitem.itemssmb.ItemSsmb;
import net.sxnic.jhitem.itemssmb.ItemSsmbManager;
import net.sxnic.jhitem.subject.SubjectManager;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.workflow.SxnicWfException;
import net.sxnic.workflow.context.WfContextImpl;
import net.sxnic.workflow.node.Node;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.task.TaskManager;
import net.sxnic.workflow.transition.Transition;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 申报书主表
 * 
 * @author 孙宇飞
 * 
 */
@SuppressWarnings("serial")
public class MdataAction extends ItemActionSupport {

	private Logger logger = LoggerFactory.getLogger(MdataAction.class);
	private int firstPage = 26;
	// 科技攻关
	protected static String[] GGFIELD01 = { "firstUnit", "unitType",
			"unitAddress", "workerTotal", "midUp", "bank", "account",
			"adminName", "adminPhone", "adminFax", "adminPostcode", "linkman",
			"linkmanPhone", "linkmanEmail", "lyearIncome", "lyearTechincome",
			"lyearTax", "lyearProfit", "totalAsset", "lyearFlowasset",
			"lyearFixasset", "equip", "lyearDebt", "cooperation1",
			"cooperation2", "cooperation3" };
	protected static String[] GGFIELD02 = { "appName", "zzdwCode", "stime",
			"etime", "subject1Code","keyWords", "sortType", "guideField", "industry", "product",
			"expertPantent" };
	protected static String[] GGFIELD02CLOB = { "clob2", "clob3" };
	protected static String[] GGFIELD06CLOB = { "clob9", "clob10", "clob11",
			"clob12" };

	// 软科学
	protected static String[] RKXFIELD01 = { "firstUnit", "zzdwCode",
			"unitType", "unitAddress", "bank", "account", "adminName",
			"adminPhone", "adminFax", "adminPostcode", "linkman",
			"linkmanPhone", "linkmanEmail", "cooperation1", "cooperation2",
			"cooperation3", "appName", "sortType", "stime", "etime" };
	protected static String[] RKXFIELD03 = { "clob1", "clob2", "clob3",
			"clob4", "clob5", "clob6" };

	// 国际合作
	protected static String[] GJHZFIELD01 = { "firstUnit", "unitAddress",
			"adminCode", "yn", "adminName", "adminPhone", "adminFax",
			"workerTotal", "college", "developer", "linkman", "linkmanPhone",
			"linkmanFax", "bank", "account", "creditLevel", "unitProperty",
			"unitFax", "linkmanEmail", "totalAsset", "lyearAsset", "lyearDebt",
			"lyearIncome", "lyearTax", "lyearExchange" };
	protected static String[] GJHZFIELD01CLOB = { "clob3", "clob4", "clob5",
			"clob6", "clob7" };
	protected static String[] GJHZFIELD02 = { "appName", "stime", "etime",
			"plan", "techFrom","techField","govGjhz", "zzdwCode", "itemPatent" };
	protected static String[] GJHZFIELD02CLOB = {"clob1","clob2"};
	protected static String[] GJHZFIELD03 = { "addProduce", "beforeScale",
			"afterScale", "beforeUnit", "afterUnit", "addIncome", "addTax",
			"addProfit", "checkContent" };
	protected static String[] GJHZFIELD03CLOB = { "clob8", "clob9", "clob10",
			"clob11", "clob12", "clob13" };
	protected static String[] GJHZFIELD05 = { "chinName", "sex", "born",
			"techTitle", "duty", "highestOffer", "graduate", "graduateStime","graduateEtime",
			"specialty", "educateDegree", "graduate1", "graduate1Stime","graduate1Etime",
			"specialty1", "graduate1Degree", "address", "postcode", "phone",
			"mobile", "email", "idcard", "foreignCountry", "foreignUnit",
			"foreignTime", "foreignSpecialty", "achievements" };

	// 创新计划
	protected static String[] CXJHFIELD01 = { "gzdwCode", "appName",
			"unitAddress", "adminName", "adminPhone", "adminFax", "linkman",
			"linkmanPhone", "linkmanEmail", "workerTotal", "midUp",
			"lyearSell", "lyearTax", "lyearIncome", "lyearFixasset", "equip" };
	protected static String[] CXJHFIELD02 = { "planTime", "zzdwCode",
			"unitType" };
	protected static String[] CXJHFIELD02FUND = { "heji", "ziyou", "kjtbk" };
	protected static String[] CXJHFIELD02CLOB = { "clob1", "clob2" };
	protected static String[] CXJHFIELD03 = { "clob3", "clob4", "clob5",
			"clob6", "clob7", "clob8", "clob9", "clob10", "clob11", "clob12" };
	// 产业化（星火）包括大字段表
	protected static String[] XHFIELD01 = {"firstUnit","adminCode","unitType","unitAddress","workerTotal","college","developer","bank","account","creditLevel",
		"adminName","adminPhone","adminFax","adminPostcode","linkman","linkmanPhone","linkmanEmail","lyearIncome","lyearTechincome","lyearTax","lyearProfit",
		"totalAsset","lyearFlowasset","lyearFixasset","equip","lyearDebt","cooperation1","cooperation2","cooperation3",};
	protected static String[] XHFIELD01CLOB = {"clob1"};
	protected static String[] XHFIELD02 = {"appName","zzdwCode","stime","etime","techFrom","techField","techStandard","industry","judgeMode","itemAward"};
	protected static String[] XHFIELD02CLOB = {"clob3","clob12","clob13"};
	protected static String[] XHFIELD03 = {"addProduce","addTax","addProfit","beforeScale","beforeUnit","afterScale","afterUnit"};
	protected static String[] XHFIELD03CLOB = {"clob5"};
/*	protected static String[] XHFIELD05 = {"chinName","sex","born","techTitle","duty","highestOffer","graduate","graduateYear","specialty","address","postcode","phone","mobile","email","idcard",
		"achievements"};*/
	protected static String[] XHFIELD06 = {"clob8","clob9","clob10","clob11"};
	// 公用项目实施
  
	// 公用person情况
	protected static String[] GYPER = { "chinName", "sex", "born", "techTitle",
			"highestOffer", "duty", "graduate", "graduateYear", "specialty",
			"address", "postcode", "phone", "mobile", "email", "idcard",
			"achievements", "graduateStime", "graduateEtime", "educateDegree",
			"graduate1Stime", "specialty1", "graduate1Degree",
			"foreignCountry", "foreignUnit", "foreignTime", "foreignSpecialty" };
	// 公用经费
	protected static String[] GYFUNDS = { "heji", "kjtbk", "kjtbkYear1",
			"kjtbkYear2", "kjtbkYear3", "qitabk", "qitabkGjbk", "ziyou",
			"ziyouQiye", "bankLoan", "qitazj", "zchj", "zchjKjtzx", "zjfy",
			"zjfyKjtzx", "shebei", "shebeiKjtzx", "cailiao", "cailiaoKjtzx",
			"ceshi", "ceshiKjtzx", "ranliao", "ranliaoKjtzx", "chailv",
			"chailvKjtzx", "meeting", "meetingKjtzx", "hezuo", "hezuoKjtzx",
			"chuban", "chubanKjtzx", "laowu", "laowuKjtzx", "zhuanjia",
			"zhuanjiaKjtzx", "qitazc", "qitazcKjtzx", "jjzc", "jjzcKjtzx",
			"jixiao", "jixiaoKjtzx" };

	// optCate = submit 表示提交 为空表示保存
	protected String optCate;
	// 提交时验证的提示信息
	protected String valTip = "";

	protected String html;

	protected Map<String, String> subTableHtml;

	/**
	 * 申报年份
	 */
	protected String appYear;
	// 申报年份下一年
	protected String appYearNext;
	// 申报年份以后的第二年
	protected String appYearAfterNext;
	/**
	 * 大类别名
	 */
	protected String catalogAlias;

	/**
	 * 小类编码
	 */
	protected String catalogCode;

	/**
	 * JSP对应的年份
	 */
	protected String jspYear;

	protected String jspCatalog;

	protected Mdata mdata;
	// 国际合作国内外合作单位相关内容
	protected String gwCountry1;
	protected String gwCountry2;
	protected String gwOrgname1;
	protected String gwOrgname2;
	protected String gwLeader1;
	protected String gwLeader2;
	protected String gwPhone1;
	protected String gwPhone2;
	protected String gwFax1;
	protected String gwFax2;
	protected String gwEmail1;
	protected String gwEmail2;
	protected String gnOrgname1;
	protected String gnOrgname2;
	protected String gnLeader1;
	protected String gnLeader2;
	protected String gnPhone1;
	protected String gnPhone2;
	protected String gnFax1;
	protected String gnFax2;
	protected String gnEmail1;
	protected String gnEmail2;
	// 一级学科
	protected String level_1;
	// 二级学科
	protected String level_2;
	// 三级学科
	protected String level_3;
	protected List<ItemGjhzunit> gwGjhzunitList;
	protected List<ItemGjhzunit> gnGjhzunitList;
	// 项目经费
	protected ItemFund itemFund;
	// 项目负责人
	protected ItemPrincipal itemPrincipal;
	// 项目大字段
	protected ItemClob itemClob;

	// 项目分阶段实施列表
	protected List<ItemSsmb> ssmbList;
	// 项目主要参加人员列表
	protected List<ItemPeople> peopleList;
	// 公共附件列表
	protected List<ItemAnnex> annex001List;
	// 报告正文附件列表
	protected List<ItemAnnex> annex002List;
	protected Page datas;

	protected String taskId;

	protected String buttonType;

	protected Integer pageNo;

	protected WfContextImpl<Mdata> context;

	@Autowired
	protected MdataManager mdManager;

	@Autowired
	protected TaskManager taskManager;

	@Autowired
	protected AttachmentManager attachMentManager;

	@Autowired
	protected DuiyingManager dyManager;

	@Autowired
	protected ItemFundManager fundManager;

	@Autowired
	protected ItemPrincipalManager principalManager;

	@Autowired
	protected ItemClobManager clobManager;

	@Autowired
	protected ItemPeopleManager peopleManager;

	@Autowired
	protected ItemSsmbManager ssmbManager;
	@Autowired
	protected ItemAnnexManager annexManager;
	@Autowired
	protected ItemOperationManager operationManager;
	@Autowired
	protected ItemGjhzunitManager gjhzunitManager;
	@Autowired
	protected SubjectManager subjectManager;
	@Autowired
	protected DeptManager deptManager;

	/**
	 * 把URI请求分解为 年 大类 小类，即把edit_2013_item_001.jsp分解
	 */
	protected void treatYCC() {
		String temp = request.getRequestURI();
		temp = StringUtils.substringAfter(request.getRequestURI(), "_");
		temp = StringUtils.substringBefore(temp, ".");

		appYear = StringUtils.substring(temp, 0, 4);
		catalogCode = StringUtils.substringAfterLast(temp, "_");
		catalogAlias = StringUtils.substringBetween(temp, appYear + "_", "_"
				+ catalogCode);
	}

	/**
	 * 生成主表数据
	 * 
	 * @return
	 */
	protected void creMainTableHtml() {
		PageUtils pu = new PageUtils();
		html = pu.crtMdataSingleHtml(mdata);
	}

	protected void signal(Task task, String transitionName) throws Exception {
		Node nextNode = null;

		for (Transition t : task.getNode().getTransitions()) {
			if (transitionName.equals(t.getName())) {
				nextNode = t.getTo();
			}
		}

		if (nextNode == null) {
			throw new SxnicWfException(task.getNode().getName()
					+ "对应的Node没有与之对应的Transition:" + transitionName);
		}

		logger.debug("");

		String nextAssigneeDef = nextNode.getAssignee();
		if (StringUtils.isBlank(nextAssigneeDef)) {
			nextAssigneeDef = "";
		}

		String deptCode = "";
		String nextAssignee = "";
		if (nextAssigneeDef.endsWith("gldw_g")) {
			// 如果下个节点是管理单位 gldw_g
			deptCode = mdata.getGldwCode();
			nextAssignee = ucenterService.findTaskAssignee(ucenterService
					.findDeptByCode(deptCode));
		} else if (nextAssigneeDef.endsWith("zzdw_g")) {
			// 如果下个节点是组织单位 zzdw_g
			deptCode = mdata.getZzdwCode();
			nextAssignee = ucenterService.findTaskAssignee(ucenterService
					.findDeptByCode(deptCode));
		} else if (nextAssigneeDef.endsWith("gzdw_g")) {
			// 如果下个节点是工作单位 gzdw_g
			deptCode = mdata.getGzdwCode();
			nextAssignee = ucenterService.findTaskAssignee(ucenterService
					.findDeptByCode(deptCode));
		} else if (nextAssigneeDef.endsWith("initiator")) {
			// 申报单位或个人
			deptCode = "initiator";
			nextAssignee = mdata.getApprover();
		} else {
			// 无用户定义
			deptCode = "";
			nextAssignee = "";
		}

		Task nextTask = taskSingle(task, nextNode, nextAssignee,
				task.getComments());

		// 为mdata设置新的状态
		mdata.setStatus(nextTask.getNode().getStatusName());
		mdata.setStatusOrder(nextTask.getNode().getStatusCode());
		mdManager.update(mdata);
		logger.debug("====结束当前任务成功==并更新Mdata==" + task);
	}

	protected Task taskSingle(Task task, Node nextNode, String nextAssignee,
			String comment) throws SxnicWfException {
		System.out.println("执行");
		Task nextTask = null;

		task.setComments(comment);
		task.setStatus(Task.TASK_STATUS_ENDED);
		task.setEndDate(new Date());
		// 设置当前任务为最新的任务
		task.setLastOne(true);
		taskManager.update(task);

		if (StringUtils.isBlank(nextAssignee)) {
			nextTask = new Task();
			nextTask.setNode(nextNode);
			nextTask.setFromTask(task);
			logger.debug("=====没有选择后续节点，结束当前任务====" + task);
		} else {
			nextTask = taskManager.createTask(nextNode, nextAssignee, task);
			logger.debug("=====创建任务成功====" + nextTask);

			// 设置之前的任务 不是最新任务
			DetachedCriteria dc = DetachedCriteria.forClass(Task.class);
			dc.add(Restrictions.eq("assignee", nextTask.getAssignee()));
			dc.add(Restrictions.eq("mdata", task.getMdata()));
			dc.add(Restrictions.ne("id", nextTask.getId()));
			dc.addOrder(Order.asc("creationDate"));

			List<Task> list = taskManager.getByDetachedCriteria(dc);
			for (Task t : list) {
				t.setLastOne(false);
				taskManager.update(t);
			}

			logger.debug("=====设置非当前任务==状态为：非最新task===");
		}

		return nextTask;
	}

	// 公共人员

	// 公用类的验证
	protected boolean validateGGP() {

		if("009".equals(jspCatalog))
			return true;
		boolean statu = true;
		Object text = null;

		String key = jspCatalog.trim();

		for (int i = 0; i < GYPER.length; i++) {
			text = WordUtils
					.getter(itemPrincipal, WordUtils.capFirst(GYPER[i]));
			if (text != null) {
				if (StringUtils.isBlank(text.toString())) {
					valTip = "文字内容如果没有请写无，数字内容没有请写0";

					if ("005".equals(key)) {
						pageNo = 2;
					}
					if ("009".equals(key)) {
						pageNo = 5;
					}

					if ("006".equals(key)) {
						pageNo = 5;

					}
					statu = false;
					break;
				}
			}
			
		}
		
		return statu;

	}

	// 验证经费
	protected boolean valFunds() throws Exception {
		if ("011".equals(jspCatalog)) {
			return true;
		}
		boolean statu = true;
		Object text = null;
		String key = jspCatalog.trim();
		for (int i = 0; i < GYFUNDS.length; i++) {
			text = WordUtils.getter(itemFund, WordUtils.capFirst(GYFUNDS[i]));
			if (text != null) {
				if (StringUtils.isBlank(text.toString())) {
					valTip = "文字内容如果没有请写无，数字内容没有请写0";
					   if("005".equals(key)){
						pageNo=3;
					   }else {
						   pageNo=4;
					   }
					   
					
					statu = false;
					break;
				}
			}

		}
		if (!statu)
			return statu;

		if (!valLogicFunds(itemFund)) {
			statu = false;
		}

		return statu;
	}

	// 逻辑验证经费输入
	private boolean valLogicFunds(ItemFund itemFund) throws Exception {
		String field = null;
		String[] fields = null;
		String key = jspCatalog.trim();
		// 验证项目投资和支出合计
		field = "heji";
		fields = new String[] { "zchj" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "项目投资合计和支出有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证项目投资合计
		fields = new String[] { "kjtbk", "qitabk", "ziyou", "bankLoan",
				"qitazj" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "项目投资合计有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证市科技局专项经费
		field = "kjtbk";
		fields = new String[] { "kjtbkYear1", "kjtbkYear2", "kjtbkYear3" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "市科技局专项经费有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证国家拨款
		field = "qitabk";
		fields = new String[] { "qitabkGjbk" };
		if (!validateNotLessThan(itemFund, field, fields)) {
			valTip = "国家拨款有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证自有资金
		field = "ziyou";
		fields = new String[] { "ziyouQiye" };
		if (!validateNotLessThan(itemFund, field, fields)) {
			valTip = "自有资金有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证经费支出合计
		field = "zchj";
		fields = new String[] { "zjfy", "jjzc" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "项目经费支出有误";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证支出直接费用
		field = "zjfy";
		fields = new String[] { "shebei", "cailiao", "ceshi", "ranliao",
				"chailv", "meeting", "hezuo", "chuban", "laowu", "zhuanjia",
				"qitazc" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "项目支出直接费用有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证间接费用支出
		field = "jjzc";
		fields = new String[] { "jixiao" };
		if (!validateNotLessThan(itemFund, field, fields)) {
			valTip = "间接费用支出有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证支出合计中各项直接费用是否大于等于市科技局专项经费支出
		String[][] zjFields = {
				{ "zchj", "zjfy", "shebei", "cailiao", "ceshi", "ranliao",
						"chailv", "meeting", "hezuo", "chuban", "laowu",
						"zhuanjia", "qitazc" },
				{ "项目支出合计", "直接费用", "设备费", "材料费", "测试化验加工费", "燃料动力费", "差旅费",
						"会议费", "合作研究与交流费", "出版/文献/信息传播/知识产权事务费", "劳务费",
						"专家咨询费", "其他支出" } };
		if (!validateNotLessThan(itemFund, zjFields))
			return false;
		// 验证科技局专项经费支出
		field = "zchjKjtzx";
		fields = new String[] { "zjfyKjtzx", "jjzcKjtzx" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "科技局专项经费支出合计有误";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证科技局专项经费支出直接费用
		field = "zjfyKjtzx";
		fields = new String[] { "shebeiKjtzx", "cailiaoKjtzx", "ceshiKjtzx",
				"ranliaoKjtzx", "chailvKjtzx", "meetingKjtzx", "hezuoKjtzx",
				"chubanKjtzx", "laowuKjtzx", "zhuanjiaKjtzx", "qitazcKjtzx" };
		if (!validateEqual(itemFund, field, fields)) {
			valTip = "科技局专项经费直接费用有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		// 验证科技局专项经费支出间接费用
		field = "jjzcKjtzx";
		fields = new String[] { "jixiaoKjtzx" };
		if (!validateNotLessThan(itemFund, field, fields)) {
			valTip = "科技局专项经费间接费用支出有误！";
			if (key.equals("005")) {
				pageNo = 3;
			} else {
				pageNo = 4;
			}
			return false;
		}
		return true;
	}

	// 验证是否相等
	private boolean validateEqual(ItemFund itemFund, String field,
			String... fields) throws Exception {

		Class<ItemFund> clazz = ItemFund.class;
		Method m = clazz.getMethod("get" + WordUtils.capFirst(field));
		BigDecimal left = new BigDecimal((String) m.invoke(itemFund));
		BigDecimal right = new BigDecimal("0");

		for (String s : fields) {
			Method m1 = clazz.getMethod("get" + WordUtils.capFirst(s));
			right = right.add(new BigDecimal((String) m1.invoke(itemFund)));
		}
		if (left.compareTo(right) != 0)
			return false;
		return true;
	}

	// 验证是否大于等于
	private boolean validateNotLessThan(ItemFund itemFund, String field,
			String... fields) throws Exception {

		Class<ItemFund> clazz = ItemFund.class;
		Method m = clazz.getMethod("get" + WordUtils.capFirst(field));
		BigDecimal left = new BigDecimal((String) m.invoke(itemFund));
		BigDecimal right = new BigDecimal("0");

		for (String s : fields) {
			Method m1 = clazz.getMethod("get" + WordUtils.capFirst(s));
			right = right.add(new BigDecimal((String) m1.invoke(itemFund)));
		}
		if (left.compareTo(right) == -1)
			return false;
		return true;
	}

	// 验证各项市科技局专项经费支出是否小于各项合计
	private boolean validateNotLessThan(ItemFund itemFund, String[][] fields)
			throws Exception {
		Class<ItemFund> clazz = ItemFund.class;
		Method m1 = null;
		Method m2 = null;
		String key = jspCatalog.trim();
		BigDecimal left = new BigDecimal("0");
		BigDecimal right = new BigDecimal("0");
		for (int i = 0; i < fields[0].length; i++) {
			m1 = clazz.getMethod("get" + WordUtils.capFirst(fields[0][i]));
			m2 = clazz.getMethod("get"
					+ WordUtils.capFirst(fields[0][i] + "Kjtzx"));
			left = new BigDecimal((String) m1.invoke(itemFund));
			right = new BigDecimal((String) m2.invoke(itemFund));
			if (left.compareTo(right) == -1) {
				valTip = "项目支出合计中的" + fields[1][i] + "有误";
				if (key.equals("005")) {
					pageNo = 3;
				} else {
					pageNo = 4;
				}
				return false;
			}
		}

		return true;
	}

	// 分种类验证
	protected boolean vldSubmit() {
		boolean temp = true;
		String key = jspCatalog.trim();
		switch (key) {
		case "001":
			// 攻关的验证
			temp = valKjgg();
			break;
		case "005":
			// 软科学
			temp = valRkx();
			break;
		case "006":
           //产业化
			temp = valCyhjh();
			break;
		case "009":
			temp = valGjhz();
			break;
		case "011":
			// 科技创新
			temp = valCxjh();
			break;
		default:
			break;
		}
		if (!temp) {
			valTip = "文字内容如果没有请写无，数字内容没有请写0";
		}
		return temp;
	}

	// 验证科技攻关
	protected boolean valKjgg() {
		boolean valKjgg = false;
		Object text = null;

		for (int i = 0; i < GGFIELD01.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(GGFIELD01[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 1;
				return valKjgg;
			}
		}

		for (int i = 0; i < GGFIELD02.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(GGFIELD02[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return valKjgg;
			}
		}

		for (int i = 0; i < GGFIELD02CLOB.length; i++) {
			text = WordUtils.getter(itemClob,
					WordUtils.capFirst(GGFIELD02CLOB[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return valKjgg;
			}
		}
		if (annex002List == null || annex002List.size() == 0) {
			for (int i = 0; i < GGFIELD06CLOB.length; i++) {
				text = WordUtils.getter(itemClob,
						WordUtils.capFirst(GGFIELD06CLOB[i]));
				if (StringUtils.isBlank(text.toString())) {
					pageNo = 6;
					return valKjgg;
				}
			}
		}
		if(ssmbList==null||ssmbList.size()==0){
			     pageNo=3;
			     return valKjgg;
		}
		if(peopleList==null||peopleList.size()==0){
			    pageNo = 5;
			    return valKjgg;
		}

		return true;
	}

	// 国际合作
	protected boolean valGjhz() {
		boolean val = false;
		Object text = null;
		for (int i = 0; i < GJHZFIELD01.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(GJHZFIELD01[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 1;
				return val;
			}
		}
		for (int i = 0; i < GJHZFIELD01CLOB.length; i++) {
			text = WordUtils.getter(itemClob,
					WordUtils.capFirst(GJHZFIELD01CLOB[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 1;
				return val;
			}
		}
		for (int i = 0; i < GJHZFIELD02.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(GJHZFIELD02[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return val;
			}
		}
		for (int i = 0; i < GJHZFIELD02CLOB.length; i++) {
			text = WordUtils.getter(itemClob, WordUtils.capFirst(GJHZFIELD02CLOB[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return val;
			}
		}

		// 判断国外列表是否为空
		if (StringUtils.isBlank(gwGjhzunitList.get(0).getCoCountry())
				|| StringUtils.isBlank(gwGjhzunitList.get(0).getCoOrgname())
				|| StringUtils.isBlank(gwGjhzunitList.get(0).getCoLeader())
				|| StringUtils.isBlank(gwGjhzunitList.get(0).getCoPhone())
				|| StringUtils.isBlank(gwGjhzunitList.get(0).getCoFax())
				|| StringUtils.isBlank(gwGjhzunitList.get(0).getCoEmail())
				|| StringUtils.isBlank(gwGjhzunitList.get(1).getCoCountry())
				|| StringUtils.isBlank(gwGjhzunitList.get(1).getCoOrgname())
				|| StringUtils.isBlank(gwGjhzunitList.get(1).getCoLeader())
				|| StringUtils.isBlank(gwGjhzunitList.get(1).getCoPhone())
				|| StringUtils.isBlank(gwGjhzunitList.get(1).getCoFax())
				|| StringUtils.isBlank(gwGjhzunitList.get(1).getCoEmail())) {
			pageNo = 2;
			return val;
		}
		// 判断国内列表是否为空
		if (StringUtils.isBlank(gnGjhzunitList.get(0).getCoOrgname())
				|| StringUtils.isBlank(gnGjhzunitList.get(0).getCoLeader())
				|| StringUtils.isBlank(gnGjhzunitList.get(0).getCoPhone())
				|| StringUtils.isBlank(gnGjhzunitList.get(0).getCoFax())
				|| StringUtils.isBlank(gnGjhzunitList.get(0).getCoEmail())
				|| StringUtils.isBlank(gnGjhzunitList.get(1).getCoOrgname())
				|| StringUtils.isBlank(gnGjhzunitList.get(1).getCoLeader())
				|| StringUtils.isBlank(gnGjhzunitList.get(1).getCoPhone())
				|| StringUtils.isBlank(gnGjhzunitList.get(1).getCoFax())
				|| StringUtils.isBlank(gnGjhzunitList.get(1).getCoEmail())) {
			pageNo = 2;
			return val;
		}

		for (int i = 0; i < GJHZFIELD03.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(GJHZFIELD03[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 3;
				return val;
			}
		}
		if (ssmbList == null || ssmbList.size() == 0) {
			pageNo = 3;
			return val;
		}
		for (int i = 0; i < GJHZFIELD03CLOB.length; i++) {
			text = WordUtils.getter(itemClob,
					WordUtils.capFirst(GJHZFIELD03CLOB[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 3;
				return val;
			}
		}

		for (int i = 0; i < GJHZFIELD05.length; i++) {
			text = WordUtils.getter(itemPrincipal,
					WordUtils.capFirst(GJHZFIELD05[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 5;
				return val;
			}
		}

		if (peopleList == null || peopleList.size() == 0) {
			pageNo = 5;
			return val;
		}
		return true;
	}

	protected boolean valCxjh() {
		boolean valCxjh = false;
		Object text = null;
		// 科技创新
		for (int i = 0; i < CXJHFIELD01.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(CXJHFIELD01[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 1;
				return valCxjh;
			}
		}
		for (int i = 0; i < CXJHFIELD02.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(CXJHFIELD02[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return valCxjh;
			}
		}
		for (int i = 0; i < CXJHFIELD02FUND.length; i++) {
			text = WordUtils.getter(itemFund,
					WordUtils.capFirst(CXJHFIELD02FUND[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return valCxjh;
			}
		}
		for (int i = 0; i < CXJHFIELD02CLOB.length; i++) {
			text = WordUtils.getter(itemClob,
					WordUtils.capFirst(CXJHFIELD02CLOB[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return valCxjh;
			}
		}
		// 判断是否上传报告正文，如上传则不验证报告正文字段
		if (annex002List == null || annex002List.size() == 0) {
			for (int i = 0; i < CXJHFIELD03.length; i++) {
				text = WordUtils.getter(itemClob,
						WordUtils.capFirst(CXJHFIELD03[i]));
				if (StringUtils.isBlank(text.toString())) {
					pageNo = 3;
					return valCxjh;
				}
			}
		}
		return true;
	}

	// 软科学
	protected boolean valRkx() {
		boolean valRkx = false;
		Object text = null;
		// 软科学
		for (int i = 0; i < RKXFIELD01.length; i++) {
			text = WordUtils.getter(mdata, WordUtils.capFirst(RKXFIELD01[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 1;
				return valRkx;
			}
		}
	/*	for (int i = 0; i < GYPER.length; i++) {
			text = WordUtils
					.getter(itemPrincipal, WordUtils.capFirst(GYPER[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 2;
				return valRkx;
			}
		}*/
		for (int i = 0; i < GYFUNDS.length; i++) {
			text = WordUtils.getter(itemFund, WordUtils.capFirst(GYFUNDS[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 3;
				return valRkx;
			}
		}
		// 判断子表是否为空
		if (ssmbList == null || ssmbList.size() == 0) {
			pageNo = 1;
			return valRkx;
		}
		if (peopleList == null || peopleList.size() == 0) {
			pageNo = 2;
			return valRkx;
		}
		// 判断是否上传报告正文，如上传则不验证报告正文字段
		if (annex002List == null || annex002List.size() == 0) {
			for (int i = 0; i < RKXFIELD03.length; i++) {
				text = WordUtils.getter(itemClob,
						WordUtils.capFirst(RKXFIELD03[i]));
				if (StringUtils.isBlank(text.toString())) {
					pageNo = 4;
					return valRkx;
				}
			}
		}
		return true;

	}
	//产业化
	protected boolean valCyhjh() 
	{
		boolean valCyhjh = false;
		Object text = null;
		//产业化
		for (int i = 0; i < XHFIELD01.length; i++) {
			text = WordUtils.getter(mdata,WordUtils.capFirst(XHFIELD01[i]));
			if (StringUtils.isBlank(text.toString())) 
			{
				pageNo = 1;
				return valCyhjh;
			}
		}
		for (int i = 0; i < XHFIELD01CLOB.length; i++) {
			text = WordUtils.getter(itemClob,WordUtils.capFirst(XHFIELD01CLOB[i]));
			if (StringUtils.isBlank(text.toString())) 
			{
				pageNo = 1;
				return valCyhjh;
			}
		}
		for (int i = 0; i < XHFIELD02.length; i++) {
			text = WordUtils.getter(mdata,WordUtils.capFirst(XHFIELD02[i]));
			if (StringUtils.isBlank(text.toString())) 
			{
				pageNo = 2;
				return valCyhjh;
			}
		}
		for (int i = 0; i < XHFIELD02CLOB.length; i++) {
			text = WordUtils.getter(itemClob,WordUtils.capFirst(XHFIELD02CLOB[i]));
			if (StringUtils.isBlank(text.toString())) 
			{
				pageNo = 2;
				return valCyhjh;
			}
		}
		for (int i = 0; i < XHFIELD03.length; i++) {
			text = WordUtils.getter(mdata,WordUtils.capFirst(XHFIELD03[i]));
			if (StringUtils.isBlank(text.toString())) 
			{
				pageNo = 3;
				return valCyhjh;
			}
		}
		for (int i = 0; i < XHFIELD03CLOB.length; i++) {
			text = WordUtils.getter(itemClob,WordUtils.capFirst(XHFIELD03CLOB[i]));
			if (StringUtils.isBlank(text.toString())) 
			{
				pageNo = 3;
				return valCyhjh;
			}
		}
	
		//判断是否上传报告正文，如上传则不验证报告正文字段
		if(annex002List==null||annex002List.size()==0){
		for (int i = 0; i < XHFIELD06.length; i++) {
			text = WordUtils.getter(itemClob,WordUtils.capFirst(XHFIELD06[i]));
			if (StringUtils.isBlank(text.toString())) {
				pageNo = 6;
				return valCyhjh;
			}
		}
		}
		
		if(ssmbList==null||ssmbList.size()==0){
		     pageNo=3;
		     return valCyhjh;
		}
		if(peopleList==null||peopleList.size()==0){
			    pageNo = 5;
			    return valCyhjh;
		}
		return true;
	}

	/**
	 * 从属性文件中获取本类别对应的申报起始时间。精确到秒。如果没有定义限制条件，则视为无时间限制。
	 * 如果年份为0000，则视为每年的这个时间段，比较时自动先把0000替换为当前年 注意：此方法主要在preCreate && create
	 * Action中使用，因为申报时限还未写到mainData中去。
	 * 
	 * @return 如果在申报时限内则返回true，否则返回false
	 */
	protected boolean checkAppDate() {

		// 从属性表中获取类别对应的起始时间
		String strStart = CommConstant.PROPERTY_MAP
				.get("item.approver.startdate");

		String strEnd = CommConstant.PROPERTY_MAP.get("item.approver.enddate");

		// 都为空则视为无限制
		if (StringUtils.isBlank(strStart) && StringUtils.isBlank(strEnd)) {
			return true;
		}

		Calendar cal = Calendar.getInstance();
		String currYear = String.valueOf(cal.get(Calendar.YEAR));
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		Date currDate = new Date();

		// 起始时间不为空，结束时间为空
		if (StringUtils.isNotBlank(strStart) && strStart.startsWith("0000")) {
			strStart = StringUtils.replace(strStart.trim(), "0000", currYear);
		}

		// 起始时间为空，结束时间不为空
		if (StringUtils.isNotBlank(strEnd) && strEnd.startsWith("0000")) {
			strEnd = StringUtils.replace(strEnd.trim(), "0000", currYear);
		}
		try {
			if (currDate.after(format.parse(strStart))
					&& currDate.before(format.parse(strEnd))) {
				return true;
			} else if (currDate.before(format.parse(strStart))) {
				msg = "maindata.action.notArriveProposeDate";
			} else if (currDate.after(format.parse(strEnd))) {
				msg = "maindata.action.beyondProposeDate";
			}
		} catch (Exception ex) {
			ex.fillInStackTrace();
		}

		return false;
	}

	protected String getJhYear(String fyear) {
		String jhYear = "";
		// 大类申报书与当前年的关系 -1=表示前一年 0=当前年 1表示后一年
		String optYearSet = PropertyUtil.getProperty("item.optYear");
		if (StringUtils.isBlank(optYearSet))
			optYearSet = "0";// 默认当前年=0
		jhYear = Integer.toString(Integer.parseInt(fyear)
				+ Integer.parseInt(optYearSet));
		return jhYear;
	}

	public String getAppYear() {
		return appYear;
	}

	public void setAppYear(String appYear) {
		this.appYear = appYear;
	}

	public String getCatalogAlias() {
		return catalogAlias;
	}

	public void setCatalogAlias(String catalogAlias) {
		this.catalogAlias = catalogAlias;
	}

	public String getCatalogCode() {
		return catalogCode;
	}

	public void setCatalogCode(String catalogCode) {
		this.catalogCode = catalogCode;
	}

	public Mdata getMdata() {
		return mdata;
	}

	public void setMdata(Mdata mdata) {
		this.mdata = mdata;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getButtonType() {
		return buttonType;
	}

	public void setButtonType(String buttonType) {
		this.buttonType = buttonType;
	}

	public WfContextImpl<Mdata> getContext() {
		return context;
	}

	public void setContext(WfContextImpl<Mdata> context) {
		this.context = context;
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public Map<String, String> getSubTableHtml() {
		return subTableHtml;
	}

	public void setSubTableHtml(Map<String, String> subTableHtml) {
		this.subTableHtml = subTableHtml;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public String getOptCate() {
		return optCate;
	}

	public void setOptCate(String optCate) {
		this.optCate = optCate;
	}

	public String getJspYear() {
		return jspYear;
	}

	public void setJspYear(String jspYear) {
		this.jspYear = jspYear;
	}

	public Page getDatas() {
		return datas;
	}

	public void setDatas(Page datas) {
		this.datas = datas;
	}

	public String getJspCatalog() {
		return jspCatalog;
	}

	public void setJspCatalog(String jspCatalog) {
		this.jspCatalog = jspCatalog;
	}

	public ItemFund getItemFund() {
		return itemFund;
	}

	public void setItemFund(ItemFund itemFund) {
		this.itemFund = itemFund;
	}

	public ItemPrincipal getItemPrincipal() {
		return itemPrincipal;
	}

	public void setItemPrincipal(ItemPrincipal itemPrincipal) {
		this.itemPrincipal = itemPrincipal;
	}

	public List<ItemSsmb> getSsmbList() {
		return ssmbList;
	}

	public void setSsmbList(List<ItemSsmb> ssmbList) {
		this.ssmbList = ssmbList;
	}

	public List<ItemPeople> getPeopleList() {
		return peopleList;
	}

	public void setPeopleList(List<ItemPeople> peopleList) {
		this.peopleList = peopleList;
	}

	public ItemClob getItemClob() {
		return itemClob;
	}

	public void setItemClob(ItemClob itemClob) {
		this.itemClob = itemClob;
	}

	public String getValTip() {
		return valTip;
	}

	public void setValTip(String valTip) {
		this.valTip = valTip;
	}

	public String getAppYearNext() {
		return String.valueOf((Integer.valueOf(appYear) + 1));
	}

	public String getAppYearAfterNext() {
		return String.valueOf((Integer.valueOf(appYear) + 2));
	}

	public String getGwCountry1() {
		return gwCountry1;
	}

	public void setGwCountry1(String gwCountry1) {
		this.gwCountry1 = gwCountry1;
	}

	public String getGwCountry2() {
		return gwCountry2;
	}

	public void setGwCountry2(String gwCountry2) {
		this.gwCountry2 = gwCountry2;
	}

	public String getGwOrgname1() {
		return gwOrgname1;
	}

	public void setGwOrgname1(String gwOrgname1) {
		this.gwOrgname1 = gwOrgname1;
	}

	public String getGwOrgname2() {
		return gwOrgname2;
	}

	public void setGwOrgname2(String gwOrgname2) {
		this.gwOrgname2 = gwOrgname2;
	}

	public String getGwLeader1() {
		return gwLeader1;
	}

	public void setGwLeader1(String gwLeader1) {
		this.gwLeader1 = gwLeader1;
	}

	public String getGwLeader2() {
		return gwLeader2;
	}

	public void setGwLeader2(String gwLeader2) {
		this.gwLeader2 = gwLeader2;
	}

	public String getGwPhone1() {
		return gwPhone1;
	}

	public void setGwPhone1(String gwPhone1) {
		this.gwPhone1 = gwPhone1;
	}

	public String getGwPhone2() {
		return gwPhone2;
	}

	public void setGwPhone2(String gwPhone2) {
		this.gwPhone2 = gwPhone2;
	}

	public String getGwFax1() {
		return gwFax1;
	}

	public void setGwFax1(String gwFax1) {
		this.gwFax1 = gwFax1;
	}

	public String getGwFax2() {
		return gwFax2;
	}

	public void setGwFax2(String gwFax2) {
		this.gwFax2 = gwFax2;
	}

	public String getGwEmail1() {
		return gwEmail1;
	}

	public void setGwEmail1(String gwEmail1) {
		this.gwEmail1 = gwEmail1;
	}

	public String getGwEmail2() {
		return gwEmail2;
	}

	public void setGwEmail2(String gwEmail2) {
		this.gwEmail2 = gwEmail2;
	}

	public String getGnOrgname1() {
		return gnOrgname1;
	}

	public void setGnOrgname1(String gnOrgname1) {
		this.gnOrgname1 = gnOrgname1;
	}

	public String getGnOrgname2() {
		return gnOrgname2;
	}

	public void setGnOrgname2(String gnOrgname2) {
		this.gnOrgname2 = gnOrgname2;
	}

	public String getGnLeader1() {
		return gnLeader1;
	}

	public void setGnLeader1(String gnLeader1) {
		this.gnLeader1 = gnLeader1;
	}

	public String getGnLeader2() {
		return gnLeader2;
	}

	public void setGnLeader2(String gnLeader2) {
		this.gnLeader2 = gnLeader2;
	}

	public String getGnPhone1() {
		return gnPhone1;
	}

	public void setGnPhone1(String gnPhone1) {
		this.gnPhone1 = gnPhone1;
	}

	public String getGnPhone2() {
		return gnPhone2;
	}

	public void setGnPhone2(String gnPhone2) {
		this.gnPhone2 = gnPhone2;
	}

	public String getGnFax1() {
		return gnFax1;
	}

	public void setGnFax1(String gnFax1) {
		this.gnFax1 = gnFax1;
	}

	public String getGnFax2() {
		return gnFax2;
	}

	public void setGnFax2(String gnFax2) {
		this.gnFax2 = gnFax2;
	}

	public String getGnEmail1() {
		return gnEmail1;
	}

	public void setGnEmail1(String gnEmail1) {
		this.gnEmail1 = gnEmail1;
	}

	public String getGnEmail2() {
		return gnEmail2;
	}

	public void setGnEmail2(String gnEmail2) {
		this.gnEmail2 = gnEmail2;
	}

	public List<ItemGjhzunit> getGwGjhzunitList() {
		return gwGjhzunitList;
	}

	public void setGwGjhzunitList(List<ItemGjhzunit> gwGjhzunitList) {
		this.gwGjhzunitList = gwGjhzunitList;
	}

	public List<ItemGjhzunit> getGnGjhzunitList() {
		return gnGjhzunitList;
	}

	public void setGnGjhzunitList(List<ItemGjhzunit> gnGjhzunitList) {
		this.gnGjhzunitList = gnGjhzunitList;
	}

	public String getLevel_1() {
		return level_1;
	}

	public void setLevel_1(String level_1) {
		this.level_1 = level_1;
	}

	public String getLevel_2() {
		return level_2;
	}

	public void setLevel_2(String level_2) {
		this.level_2 = level_2;
	}

	public String getLevel_3() {
		return level_3;
	}

	public void setLevel_3(String level_3) {
		this.level_3 = level_3;
	}

	public List<ItemAnnex> getAnnex001List() {
		return annex001List;
	}

	public void setAnnex001List(List<ItemAnnex> annex001List) {
		this.annex001List = annex001List;
	}

	public List<ItemAnnex> getAnnex002List() {
		return annex002List;
	}

	public void setAnnex002List(List<ItemAnnex> annex002List) {
		this.annex002List = annex002List;
	}

}
